package com.ruoyi.minio;

import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.support.http.stat.WebAppStat;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.Filter;
import java.lang.reflect.Field;

/**
 * @Author alison
 * @Date 2024/12/15 17:51
 * @Version 1.0
 * @Description
 */
@Component
public class ExtendConfig implements InitializingBean {
    public FilterRegistrationBean<WebStatFilter> customWebStatFilterRegistrationBean() {
        // 原来的bean创建逻辑
        FilterRegistrationBean<WebStatFilter> registrationBean = new FilterRegistrationBean<>();
        WebStatFilter webStatFilter = new WebStatFilter();
        // 设置参数示例，需确保这些参数设置正确，可能影响webAppStat的创建
//        webStatFilter.setStatViewServletUrl("/druid/*");
        registrationBean.setFilter(webStatFilter);
        registrationBean.addUrlPatterns("/*");
        return registrationBean;
    }

//    @Resource(name = "webStatFilterRegistrationBean")
//    private FilterRegistrationBean<WebStatFilter> webStatFilterRegistrationBean;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        FilterRegistrationBean webStatFilterRegistrationBean = (FilterRegistrationBean) applicationContext.getBean("webStatFilterRegistrationBean");
        Filter filter = webStatFilterRegistrationBean.getFilter();
        Field webAppStat = ReflectUtils.getAccessibleField(filter, "webAppStat");
        webAppStat.set(filter, new WebAppStat());
        System.out.println("webStatFilterRegistrationBean = " + webStatFilterRegistrationBean);
    }
}
